package com.cn.struts2;

import java.util.List;

import cn.com.allunion.common.UUIDGenerator;
import cn.com.allunion.common.page.PageView;
import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.pojo.Smsbusiness;
import cn.com.allunion.sms.pojo.Smsoperator;
import cn.com.allunion.sms.pojo.Smstimer;
import cn.com.allunion.sms.pojo.Userbase;
import cn.com.allunion.sms.service.mybatis.SmsbusinessService;
import cn.com.allunion.sms.service.mybatis.SmsoperatorService;
import cn.com.allunion.sms.service.mybatis.SmstimerService;
import cn.com.allunion.sms.service.mybatis.UserbaseService;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.transaction.annotation.Transactional;

import com.cn.entity.SMSBusiness;
import com.cn.entity.SMSOperator;
import com.cn.entity.SMSTimer;
import com.cn.entity.UserBase;
import com.cn.quartz.QuartzManage;

public class SMSSendTimerAction extends BaseAction {
    /**
     * 随机序列号
     */
    private static final long serialVersionUID = 7038781359404888651L;
    /**
     * 打印日志
     */
    private Logger logger = Logger.getLogger(SMSSendTimerAction.class);

    /**
     * 页面大小
     */
    public int pageSize;

    private SmsoperatorService smsoperatorService ;

    private SmsbusinessService smsbusinessService ;

    private UserbaseService userbaseService ;

    private SmstimerService smstimerService ;

    /**
     * 添加定时记录
     *
     * @param json "{'businessRecord':'TIETIE','smsOperator':'QIXT#','sendContent':'梅泰诺01','sendNumber':'13031039465,13146185005'}"
     *             格式
     * @param time 定时时间
     * @return String:failure or success
     * @throws JSONException
     */
    public String addSMSTimer(String json, String time, String jobName, String userID)
            throws JSONException {
        JSONObject item = new JSONObject(json);

        Smstimer st = new Smstimer();
        st.setSmsTimerID(UUIDGenerator.generatorRandomUUID());
        st.setReceiveNum(item.getString("sendNumber"));
        st.setSmsContext(item.getString("sendContent"));
        st.setJobName(jobName);
        st.setTimingDate(QuartzManage.convDate(time));
        Smsoperator smsOperator = smsoperatorService.findOperatorObj(item.getString("smsOperator")) ;

        if (null != smsOperator) {
            st.setSmsOperator(smsOperator.getSmsOperatorID());
        }

        List<Smsbusiness> smsBusiness = smsbusinessService.findbyCode(item.getString("businessRecord")) ;

        if (smsBusiness != null && smsBusiness.size() != 0) {
            st.setSmsBusiness(smsBusiness.get(0).getSmsBusinessID());
        }
        Userbase ub = userbaseService.findById(userID) ;
        st.setUserBase(ub.getUserId());
        boolean result = smstimerService.save(st) ;
        return result ? "success" : "failure";
    }

    /**
     * 修改定时记录
     *
     * @param time
     * @param jobName
     * @return String:failure or success
     */
    public String modifySMSTimer(String jobName, String time) {
        List<Smstimer> smsSendTimer = smstimerService.findByJobName(jobName) ;
        Smstimer st;
        if (smsSendTimer == null || smsSendTimer.size() == 0) {
            return "failure";
        } else {
            st = smsSendTimer.get(0);
            st.setTimingDate(QuartzManage.convDate(time));
        }
        boolean result = smstimerService.update(st) ;
        return result ? "success" : "failure";
    }

    /**
     * 删除定时记录
     *
     * @param jobName
     * @return String:failure or success
     * @author wzx
     */
    public String deleSMSTimer(String jobName) {
        List<Smstimer> smsSendTimer = smstimerService.findByJobName(jobName);
        Smstimer st;
        if (smsSendTimer == null || smsSendTimer.size() == 0) {
            return "failure";
        } else {
            st = smsSendTimer.get(0);
        }
        boolean result = smstimerService.deleteById(st.getSmsTimerID()) ;
        return result ? "success" : "failure";
    }

    /**
     * 取消定时记录
     *
     * @param jobName
     * @return String:failure or success
     */
    public String cancelSMSTimer(String jobName) {
        List<Smstimer> smsSendTimer = smstimerService.findByJobName(jobName);
        Smstimer st;
        if (smsSendTimer == null || smsSendTimer.size() == 0) {
            return "failure";
        } else {
            st = smsSendTimer.get(0);
            st.setState("2");
        }
        boolean result = smstimerService.update(st) ;
        return result ? "success" : "failure";
    }

    /**
     * 按条件查询记录
     *
     * @param createStartTime 起始创建时间
     * @param createEndTime   最后创建时间
     * @param timingStartTime 起始发送时间
     * @param timingEndTime   最后发送时间
     * @param receiveNum      接收手机号
     * @param smsContext      发送内容
     * @param state           发送状态
     * @return 符合条件的记录列表
     */
    public List<Smstimer> findSMSTimer(String createStartTime,
                                       String createEndTime, String timingStartTime, String timingEndTime,
                                       String receiveNum, String smsContext, String state, String userID) {
        return smstimerService.findByCriteria(userID, createStartTime, createEndTime, timingStartTime, timingEndTime,
                receiveNum, smsContext, state);
    }

    /**
     * 按条件查询记录
     *
     * @param createStartTime 起始创建时间
     * @param createEndTime   最后创建时间
     * @param timingStartTime 起始发送时间
     * @param timingEndTime   最后发送时间
     * @param receiveNum      接收手机号
     * @param smsContext      发送内容
     * @param state           发送状态
     * @param pageCurrent     当前页数
     * @return 符合条件的记录列表
     */
    public PageView<Smstimer> findSMSTimer(String createStartTime,
                                           String createEndTime, String timingStartTime, String timingEndTime,
                                           String receiveNum, String smsContext, String state,
                                           int pageCurrent, String userID) {
        QueryResult<Smstimer> queryResult = smstimerService.findByCriteria(userID, createStartTime, createEndTime,
                timingStartTime, timingEndTime, receiveNum, smsContext, state, pageCurrent, pageSize) ;
        PageView<Smstimer> pageView = new PageView<Smstimer>(pageSize, pageCurrent) ;
        pageView.setQueryResult(queryResult);

        return pageView;

    }

    /**
     * 相对位置
     *
     * @param count    总条数
     * @param pageSize 每页显示页数
     * @return 相对位置
     */
    public int lastPage(int count, int pageSize) {
        if (count % pageSize == 0) {
            return count / pageSize;
        }

        return count / pageSize + 1;
    }

    /**
     * 修改定时器发送状态
     *
     * @param jobName 定时器名称
     * @param state   发送状态
     * @return 方法执行结果
     */
    public String modifyState(String jobName, String state) {
        logger.debug("jobName=" + jobName + ";state=" + state);
        List<Smstimer> smsSendTimer = smstimerService.findByJobName(jobName);
        Smstimer st;
        if (smsSendTimer == null || smsSendTimer.size() == 0) {
            return "failure";
        } else {
            st = smsSendTimer.get(0);
            st.setState(state);
        }
        return smstimerService.update(st) ? "success" : "failure";

    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public SmsoperatorService getSmsoperatorService() {
        return smsoperatorService;
    }

    public void setSmsoperatorService(SmsoperatorService smsoperatorService) {
        this.smsoperatorService = smsoperatorService;
    }

    public SmsbusinessService getSmsbusinessService() {
        return smsbusinessService;
    }

    public void setSmsbusinessService(SmsbusinessService smsbusinessService) {
        this.smsbusinessService = smsbusinessService;
    }

    public UserbaseService getUserbaseService() {
        return userbaseService;
    }

    public void setUserbaseService(UserbaseService userbaseService) {
        this.userbaseService = userbaseService;
    }

    public SmstimerService getSmstimerService() {
        return smstimerService;
    }

    public void setSmstimerService(SmstimerService smstimerService) {
        this.smstimerService = smstimerService;
    }
}
